package com.isunland.managesystem.ui;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.support.v7.app.ActionBar;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListView;
import com.google.gson.Gson;
import com.isunland.managesystem.adapter.WrokProcessFinishAdapter;
import com.isunland.managesystem.base.BaseListFragment;
import com.isunland.managesystem.base.BaseVolleyActivity;
import com.isunland.managesystem.common.ApiConst;
import com.isunland.managesystem.entity.WorkprocessFinish;
import com.isunland.managesystem.entity.WorkprocessFinish.FinishDtail;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * 获取已完成工作列表
 *
 * @author wu
 */
public class WorkProcessFinishListFragment extends BaseListFragment {

  ArrayList<FinishDtail> mFinishDetail;
  private WrokProcessFinishAdapter mAdapter;

  @Override public void onCreate(Bundle savedInstanceState) {
    setHasOptionsMenu(true);
    ActionBar actionBar = ((BaseVolleyActivity) getActivity()).getSupportActionBar();
    if (actionBar != null) {

      if (NavUtils.getParentActivityName(getActivity()) != null) {
        actionBar.setDisplayHomeAsUpEnabled(true);
      }
    }
    super.onCreate(savedInstanceState);
  }

  @Override public void initData() {
    super.initData();
    setIsPaging(true);
  }

  @Override public String getUrl() {

    return ApiConst.URL_HS_FINISH_WORK;
  }

  @Override public HashMap<String, String> getparameters() {
    HashMap<String, String> params = new HashMap<>();
    params.put("index", "0");
    params.put("orderField", "PLAN_KIND_TYPE,PLAN_APPDEPT_NAME,ORDER_NO");
    params.put("orderSeq", "desc");
    return params;
  }

  @Override public void resolveResponse(String arg0) {
    WorkprocessFinish finish = new Gson().fromJson(arg0, WorkprocessFinish.class);
    List<FinishDtail> finishDetail = finish.getDataList();
    setAdapter(finishDetail);
  }

  private void setAdapter(List<FinishDtail> finishDetail) {
    if (mFinishDetail == null) {
      mFinishDetail = new ArrayList<>();
    }
    if (isPaging() && getCurrentPage() == 1) {

      mFinishDetail.clear();
    }
    mFinishDetail.addAll(finishDetail);
    if (mAdapter == null) {
      mAdapter = new WrokProcessFinishAdapter(mActivity, mFinishDetail);
      setListAdapter(mAdapter);
    }
    ((WrokProcessFinishAdapter) getListAdapter()).notifyDataSetChanged();
  }

  @Override public void onListItemClick(ListView l, View v, int position, long id) {
    FinishDtail detail = mAdapter.getItem(position - 1);
    Intent intent = new Intent(getActivity(), WorkProcessFinishDetailActicity.class);
    intent.putExtra(WorkProcessFinishDetailFragment.EXTRA_VALUE, detail);
    startActivity(intent);
    super.onListItemClick(l, v, position, id);
  }

  /**
   * 响应ActionBar中的按钮
   */
  @Override public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
      case android.R.id.home:
        if (NavUtils.getParentActivityName(getActivity()) != null) {
          NavUtils.navigateUpFromSameTask(getActivity());
        }
        break;
      default:
        return super.onOptionsItemSelected(item);
    }
    return super.onOptionsItemSelected(item);
  }
}
